﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;
using System.Net.Mail;


namespace outREACH
{
    public partial class MedicalRResources : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                Label lbl1 = (Label)Master.FindControl("Label1");
                    lbl1.Text = "Welcome " + Session["Username"].ToString() + "!";
                    Label lbl2 = (Label)Master.FindControl("Name");
                    Users u = Users.GetName(Session["Username"].ToString());
                    lbl2.Text = u.firstName + " " + u.lastName;
                    Label lbl3 = (Label)Master.FindControl("Country");
                    lbl3.Text = Session["country"].ToString();
                    Button btn3 = (Button)Master.FindControl("Button4");
                    if (User.IsInRole("admin"))
                    {
                        btn3.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            
        }

        protected void btnRequest_Click(object sender, EventArgs e)
        {
            MedResource md = new MedResource();
            md.userName = Session["Username"].ToString();
            md.medicineName = TextBox1.Text;
            md.medicineType = DropDownList1.SelectedValue.ToString();
            md.quantity = Convert.ToInt32(TextBox2.Text);
            md.reason = TextBox5.Text;
            md.dateTime = DateTime.Now;
            md.HospitalName = TextBox3.Text;
            md.category = "Medical";
            md.status = "Pending";
            md.partial = 0;
            List<Users> result = Users.GetUser();
            foreach (Users u in result)
            {
                if(u.userName == md.userName)
                {
                    md.country = u.country;
                }
            }
            MedResource.NewRequest(md);

            MailMessage message = new MailMessage();
            message.From = new MailAddress("outreach360@gmail.com");
            message.To.Add(new MailAddress("outreach360@gmail.com"));

            message.Subject = "Medical Donation has been requested";
            message.Body = "Dear Outreach, " + TextBox3.Text.ToString() + " made a request." + "\n" + "\nThey are in need of " + TextBox2.Text.ToString() + " of " + DropDownList1.SelectedValue.ToString() + "\nThe Resource Name is: " + TextBox1.Text.ToString();

            SmtpClient smtp = new SmtpClient();

            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("outreach360@gmail.com", "360outreach");

            smtp.Send(message);

            Response.Redirect("~/Medical_DCs/MedicalResource.aspx");
        }


        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}